/**
 * @sapphire.shand
 * HTTP 静态文件服务器
 */
var http = require("http"), fs = require("fs"), path = require("path"), url = require("url");
var mime = require("./mime").types;
var config = require("./config");
//var zlib = require("zlib");//使用gzip壓縮 TODO

var server = http.createServer(function(request, response){
	
	console.log('=========server=========');
	
    var pathname = url.parse(request.url).pathname;
    var reg = {
		css: '-min.css',
		js: '-min.js'
	}
    var realPath;
    var devpathname;
	var fileName;
	var lastName = {
		css:'.css',
		js :'.js'
	}
	
    if (pathname.substring(0, 5) == '/alp/') {
		realPath = path.join(config.Dev.path, pathname.substring(5));
		
	} else {
		realPath = path.join(config.Dev.path, pathname);
	}

    if (config.Dev.open) {
		console.log('config.Dev.open====' + config.Dev.open);
        var type = path.extname(realPath);
        type = type ? type.slice(1) : 'unknown';
        var folder = config.Dev.folder[type];
		folder = folder ? folder : [];
		console.log('realPath====' + realPath);
		if (folder.length > 0 && realPath.indexOf(folder[0]) > 0) {
		
			console.log('本地文件被请求====' + realPath);
			
			
			console.log('folder[0]==' + folder[0]);
//			if (realPath.indexOf(folder[0]) > 0) {
			
			
				var basename = path.basename(realPath);
				var dirname = path.dirname(realPath);
				
				//		console.log('basename=='+basename);
				basename = basename.replace(reg[type], lastName[type]);
				dirname = dirname.replace(folder[0], folder[1]);
				realPath = path.join(dirname, basename);
				console.log('realPath==' + realPath);
//			}
		}
    }
	
    path.exists(realPath, function(exists){
        if (!exists) {
            response.writeHead(404, {
                'Content-Type': 'text/plain'
            });
            
            response.write("This request URL " + pathname + " was not found on this server.");
            response.end();
        }
        else {
            fs.readFile(realPath, "binary", function(err, file){
                if (err) {
                    response.writeHead(500, {
                        'Content-Type': 'text/plain'
                    });
                    
                    response.end(err);
                }
                else {
                
                    var ext = path.extname(realPath);
                    ext = ext ? ext.slice(1) : 'unknown';
                    
                    //					//后缀名符合要添加过期时间头的請求的處理
                    //					if (ext.match(config.Expires.fileMatch)) {
                    //					    var expires = new Date();
                    //					    expires.setTime(expires.getTime() + config.Expires.maxAge * 1000);
                    //					    response.setHeader("Expires", expires.toUTCString());
                    //					    response.setHeader("Cache-Control", "max-age=" + config.Expires.maxAge);						
                    //					}
                    //					
                    //					//为所有请求的响应都添加Last-Modified头
                    //					fs.stat(realPath, function (err, stat) {
                    //					    var lastModified = stat.mtime.toUTCString();
                    //					    response.setHeader("Last-Modified", lastModified);
                    //					});
                    //					
                    //					//检测浏览器是否发送了If-Modified-Since请求头。如果发送而且跟文件的修改时间相同的话，返回304状态
                    //					if (request.headers[ifModifiedSince] && lastModified == request.headers[ifModifiedSince]) {
                    //					    response.writeHead(304, "Not Modified");
                    //					    response.end();
                    //					}					
                    
                    
                    
                    
                    var contentType = mime[ext] || "text/plain";
                    response.writeHead(200, {
                        'Content-Type': contentType,
                        'Accept-Charset': 'utf-8'
                    });
                    response.write(file, "binary");
                    response.end();
                }
            });
        }
    });
});

server.listen(config.Dev.port);
console.log("静态资源服务器已启动，端口" + config.Dev.port);

console.log(config.Dev.open?'当前模式为开发模式':'当前模式为生产模式');